package edu.friday.controller;

import edu.friday.common.constant.Constants;
import edu.friday.common.result.RestResult;
import edu.friday.common.security.LoginUser;
import edu.friday.common.security.service.SysLoginService;
import edu.friday.common.security.service.SysPermissionService;
import edu.friday.common.security.service.TokenService;
import edu.friday.model.vo.SysMenuVO;
import edu.friday.model.vo.SysUserVO;
import edu.friday.service.SysMenuService;
import edu.friday.utils.http.ServletUtils;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private SysMenuService menuService;
    @PostMapping({"/login","/"})
    public RestResult login(String userName,String password,String uuid){
        RestResult ajax=RestResult.success();
        String token=loginService.login(userName,password,"123",uuid);
        ajax.put(Constants.TOKEN,token);
        return ajax;
    }
    @GetMapping("getInfo")
    public RestResult getInfo(){
        LoginUser loginUser=tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserVO userVO=loginUser.getUserVO();
        Set<String> roles=permissionService.getRolePermission(userVO);
        Set<String> permissions=permissionService.getMenuPermission(userVO);
        RestResult ajax=RestResult.success();
        ajax.put("user", userVO);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }
    @GetMapping("getRouters")
    public RestResult getRouters(){
        LoginUser loginUser=tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserVO userVO=loginUser.getUserVO();
        List<SysMenuVO> menus=menuService.selectMenuTreeByUserId(userVO.getUserId());
        return RestResult.success(menuService.buildMenus(menus));
    }
}